﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;

namespace YiYiTong.Rocket.Helper
{
    /// <summary>
    /// </summary>
    public class DecimalFormatJsonConverter : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            if (value is decimal)
            {
                var d = (decimal)value;
                var dstr = d.ToString("#0.########");
                writer.WriteValue(decimal.Parse(dstr));
                return;
            }
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue,
            JsonSerializer serializer)
        {
            throw new NotImplementedException("Unnecessary because CanRead is false. The type will skip the converter.");
        }

        public override bool CanRead
        {
            get { return false; }
        }

        public override bool CanConvert(Type objectType)
        {
            return objectType == typeof(decimal);
        }
    }
}